home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12004 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!mikenann
  3. From: Michael Glassman and Ann Ross <mikenann@netcom.com>
  4. Subject: Re: SEX & C++
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <314C48EE.6223@netcom.com>
  7. Sender: mikenann@netcom10.netcom.com
  8. Content-Transfer-Encoding: 7bit
  9. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  10. References: <4idbcv$ue2@news7.erols.com> <4ih7gp$tdt@news5.erols.com>
  11. Mime-Version: 1.0
  12. Date: Sun, 17 Mar 1996 17:16:30 GMT
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Chris Cobb wrote:
  16. > OK, so the subject line is only half true.  But now that I have your
  17. > attention, can someone please tell me if the following code works on
  18. > their compiler and/or if the code is conformant ANSI C++.  I have tried
  19. > it on three recent-version C++ compilers and it only works on one of
  20. > them.  Since this is such a simple example and will only take you a
  21. > couple of minuites to enter into your compiler, I challenge you all to
  22. > find out if this works on your compiler.
  23. > The implications are tremendous.  If I have a constant, MYCONST, that is
  24. > defined in a header xxx.h, and let's say twenty-five other source files
  25. > use it: if I change MYCONST, then I have to recompile twenty-five source
  26. > files.
  27. > In the following scenario, I only have to recompile ONE source file, and
  28. > all of the rest can be used without recompiling.  Try it out!
  29. > --- xxx.h ---
  30. > extern const int MYCONST; // can you declare a const extern?
  31. > --- end xxx.h ---
  32. > --- xxx.cc ---
  33. > #include "xxx.h"
  34. > const int MYCONST = 11;   // const defined in one file...
  35. > --- end xxx.cc ---
  36. > --- yyy.cc ---
  37. > #include <iostream.h>
  38. > #include "xxx.h"
  39. > main()
  40. > {
  41. >    char myarray[MYCONST];  // and used in another!
  42. >    cout << "MYCONST is " << MYCONST << endl;
  43. >    cout << "sizeof myarray[] is " << sizeof(myarray) << endl;
  44. >    return 0;
  45. > }
  46. > --- end yyy.c ---
  47. > Chris
  48. > ccobb@cseg.com
  49.  
  50. It should not work an any of the compilers.  The problem is that 'const' in C++ defaults 
  51. to 'static' which means file scope in this case. You must add 'extern' to the 
  52. declaration and definition, (i.e. the .cc and .h file).
  53.  
  54. Michael Glassman
  55.